﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using Gatheshop.Common;

namespace Gatheshop.Web.admin.giftcard
{
    public partial class autoGenerate : Web.UI.ManagePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ChkAdminLevel("giftcard", GEShopEnums.ActionEnum.Edit.ToString()); //检查权限
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("cardNo");
            dt.Columns.Add("cardPwd");
            dt.Columns.Add("count");
            dt.Columns.Add("createTime");
            int num = 10;
            if (this.txtNum.Text != "")
            {
                num = Convert.ToInt32(this.txtNum.Text);
            }
            int count = 0;
            if (this.txtCount.Text != "")
            {
                count = Convert.ToInt32(this.txtCount.Text);
            }
             BLL.gift_card bll = new BLL.gift_card();
             int maxCardNo = bll.getMaxCardNo();

             for (int i = 1; i <= num; i++)
            {
               
                DataRow dr = dt.NewRow();
                dr["id"] = i;
                dr["cardNo"] ="zw"+(maxCardNo+i).ToString().PadLeft(8,'0');
                string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                string pwd =System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(time + i, "MD5");
                dr["cardPwd"] = "ZW" + pwd.Substring(pwd.Length - 8, 8);
                dr["count"] = count;
                dr["createTime"] = time;
                dt.Rows.Add(dr);
            }
             ViewState["dt"] = dt;
            this.gv_card.DataSource = dt;
            this.gv_card.DataBind();
        }

        protected void exportToExcel_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.BufferOutput = true;
            //设定输出的字符集
            Response.Charset = "UTF-8";
            //假定导出的文件名为FileName.xls
            Response.AppendHeader("Content-Disposition", "attachment;filename=giftcard.xls");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            //设置导出文件的格式
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            //关闭ViewState
            EnableViewState = false;
            System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
            System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
            gv_card.RenderControl(textWriter);

            //把HTML写回浏览器
            Response.Write(stringWriter.ToString());
            Response.End();
        }
    public override void VerifyRenderingInServerForm(Control control)

{

}

    protected void importToDB_Click(object sender, EventArgs e)
    {
        BLL.gift_card bll = new BLL.gift_card();
       
        DataTable dt = (DataTable)ViewState["dt"];
        string cardNo="";
        string cardPwd="";
        int count=0;
        string createTime = "";
        try
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cardNo = dt.Rows[i]["cardNo"].ToString();
                cardPwd = dt.Rows[i]["cardPwd"].ToString();
                count = Convert.ToInt32(dt.Rows[i]["count"].ToString());
                createTime = dt.Rows[i]["createTime"].ToString();
                bll.addGiftCard(cardNo, cardPwd, count, createTime);
            }
            JscriptMsg("礼品卡入库成功！", "list.aspx", "Success");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
     
    }
}